package com.google.code.bankagregator.adapter.bank.alpha.parser.deposit;

import com.google.code.bankagregator.adapter.bank.alpha.parser.FuncForDepParsers;
import com.google.code.bankagregator.adapter.exception.AdapterConnectException;
import com.google.code.bankagregator.entities.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kirill
 * Date: 08.06.13
 * Time: 1:52
 * To change this template use File | Settings | File Templates.
 */
public class AlphaDepSavingParser {
    private Bank bank = null;
    private final String DEPOSIT_URL = "http://www.alfabank.com.ua/retail/deposits/tariffs/program/deposits_save/";
    private Document doc = null;

    private static final int INDEX_FOR_REFILL = 2;
    private static final int INDEX_TR_FOR_TERM = 2;
    private static final int INDEX_RATE_FOR_UAH = 0;
    private static final int INDEX_RATE_FOR_USD = 1;
    private static final int INDEX_RATE_FOR_EUR = 2;

    public AlphaDepSavingParser() throws AdapterConnectException {
        bank = new Bank();
        bank.setName("ПАО «Альфа-Банк» Украина");
        bank.setUrl("http://www.alfabank.com.ua/");
        try {
            doc = Jsoup.connect(DEPOSIT_URL).timeout(20000).get();
        } catch (IOException e) {
            throw new AdapterConnectException(e.getMessage());
        }
    }

    /**
     * Get deposit`s params for UAH
     *
     * @return deposit`s params
     * @throws AdapterConnectException
     */
    public List<Deposit> getUAHDeposit() throws AdapterConnectException {
        int minAmount = Integer.parseInt(new FuncForDepParsers(DEPOSIT_URL).getMinAmount().
                substring(26, 31).replace(" ", ""));
        List<Deposit> depositParam = new AlphaDepSavingParser().parser(Currency.UAH, INDEX_RATE_FOR_UAH, minAmount);
        return depositParam;

    }

    /**
     * Get deposit`s params for USD
     *
     * @return deposit`s params
     * @throws AdapterConnectException
     */
    public List<Deposit> getUSDDeposit() throws AdapterConnectException {
        int minAmount = Integer.parseInt(new FuncForDepParsers(DEPOSIT_URL).getMinAmount().
                substring(37, 40).replace(" ", ""));
        List<Deposit> depositParam = new AlphaDepSavingParser().parser(Currency.USD, INDEX_RATE_FOR_USD, minAmount);
        return depositParam;

    }

    /**
     * Get deposit`s params for EUR
     *
     * @return deposit`s params
     * @throws AdapterConnectException
     */
    public List<Deposit> getEURDeposit() throws AdapterConnectException {
        int minAmount = Integer.parseInt(new FuncForDepParsers(DEPOSIT_URL).getMinAmount().
                substring(52, 55).replace(" ", ""));
        List<Deposit> depositParam = new AlphaDepSavingParser().parser(Currency.EUR, INDEX_RATE_FOR_EUR, minAmount);
        return depositParam;

    }

    /**
     * Get some deposit`s params
     *
     * @param currency
     * @param rateIndex - rate`s index
     * @param minAmount
     * @return some deposit`s params
     */
    private List<Deposit> parser(Currency currency, int rateIndex, int minAmount) {
        Deposit deposit = new Deposit();

        List<Deposit> depositParam = new ArrayList<>();
        Element name = doc.select("h1").first();
        Elements params = doc.select("div#content p");
        Element termEl = doc.select("table.data tr").get(INDEX_TR_FOR_TERM).select("td:not([class=top])").first();
        if (termEl.text().equals("Бессрочный")) {
            deposit.setTerm(999999);
        }

        Element refill = params.get(INDEX_FOR_REFILL);
        if (refill.text().substring(19, 30).equals("допускается")) {
            deposit.setRefill(true);
        } else {
            deposit.setRefill(false);
        }

        Element rate = doc.select("table.data tbody tr td p").get(rateIndex);
        double depRate = Double.parseDouble(rate.text().replace(",", ".").replace("%", ""));

        deposit.setType(DepositType.Deposit);
        deposit.setName(name.text());
        deposit.setCurrency(currency);
        deposit.setMinAmount(minAmount);
        deposit.setDepositRate(depRate);
        deposit.setBank(bank);
        deposit.setUrl(DEPOSIT_URL);
        deposit.setState(State.ACTIVE);
        depositParam.add(deposit);
        return depositParam;
    }
}
